angular.module("app.sys").register.controller("ListSysUserCtrl", ["$scope", "$http", "$state", "$modal", "$modalService", function($scope, $http, $state, $modal, $modalService){
  $scope.stores = [];
  $scope.pager = {
    sizeOpts: [5, 10, 15, 20, 25, 30],
    size: 15,
    maxSize: 5,
    records: 0,
    page: 1,
    onChangeSize: function(){
      $scope.pager.page = 1;
      $scope.gets(1);
    },
    onChange: function(page){
      $scope.gets(page);
    },
    total: function(){
      return Math.ceil($scope.pager.records / $scope.pager.size);
    }
  };
  $scope.getCount = function(){
    $http.post('/sys/user/count/').success(function(ret){
      if(ret.status){
        $scope.pager.records = ret.data;
      }else{
        $modalService.myAlert("获取总数失败，系统错误！");
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.gets = function(page){
    $scope.loadTip = '正在获取数据...........';
    $scope.stores = [];
    $http.post('/sys/user/gets/', {
      page: page,
      size: $scope.pager.size
    }, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $scope.stores = ret.data;
        $scope.stores.length == 0 ? $scope.loadTip = '没有找到相关记录！！' : '';
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.refresh = function(){
    $scope.stores = [];
    $scope.getCount();
    $scope.gets($scope.pager.page);
  };
  //@formatter:off
    $scope.data = {
      entity:{role_id:0,status:''},
      opts:{
        statusOpts:[{k:'',t:'请选择'},{k:1,t:'正常'},{k:0,t:'关闭'}],
        roleOpts:[]
      }
    };
    //@formatter:on
  $scope.changeStatus = function(id){
    $modalService.myConfirm("您是否确认修改用户信息状态？", function(){
      $http.post('/sys/user/changeStatus', {
        id: id
      }, {
        transformRequest: function(data){
          return $.param(data);
        }
      }).success(function(ret){
        if(ret.status){
          $scope.refresh();
        }else{
          $modalService.myAlert(ret.data);
        }
      }).error(function(){
        $modalService.myAlert('系统错误，请求资源失败！');
      });
    }, function(){
    });
  }
  //@formatter:on
  $scope.add = function(){
    if($scope.data.opts.roleOpts.length == 0){
      $http.post('/sys/user/getRoles/', {}, {
        transformRequest: function(data){
          return $.param(data);
        }
      }).success(function(ret){
        if(ret.status){
          $scope.data.opts.roleOpts.push({
            id: 0,
            name: '请选择'
          });
          $.each(ret.data, function(k, v){
            $scope.data.opts.roleOpts.push({
              id: v.id,
              name: v.name
            });
          });
        }else{
          $modalService.myAlert(ret.data);
        }
      }).error(function(){
        $modalService.myAlert('系统错误，请求资源失败！');
      });
    }
    var $modalInstance = $modal.open({
      templateUrl: "/sys/user/add/",
      controller: "AddSysUserCtrl",
      backdrop: true,
      resolve: {
        data: function(){
          return $scope.data;
        }
      }
    });
    $modalInstance.result.then(function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    }, function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    });
  };
  $scope.getCount();
  $scope.gets(1);
}]);
angular.module("app.sys").register.controller("AddSysUserCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    $http.post('/sys/user/add/', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $modalService.myAlert(ret.data);
        $modalInstance.close('success');
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
